//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "DateInterval.h"
using namespace Cephei::QL::Experimental::Commodities;
#undef HANDLE
#undef ABSTRACT
#define STRUCT
Cephei::QL::Experimental::Commodities::CDateInterval::CDateInterval (DateTime startDate, DateTime endDate) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phDateInterval = NULL;
#endif
        QuantLib::Date _startDate = (QuantLib::Date)ValueHelper::Convert (startDate);
        QuantLib::Date _endDate = (QuantLib::Date)ValueHelper::Convert (endDate);
        _ppDateInterval = new boost::shared_ptr<QuantLib::DateInterval> (new QuantLib::DateInterval ( _startDate,  _endDate ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Commodities::CDateInterval::CDateInterval () 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phDateInterval = NULL;
#endif
        _ppDateInterval = new boost::shared_ptr<QuantLib::DateInterval> (new QuantLib::DateInterval ( ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Commodities::CDateInterval::CDateInterval (boost::shared_ptr<QuantLib::DateInterval>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phDateInterval = NULL;
#endif
	_ppDateInterval = &childNative;
    
}
Cephei::QL::Experimental::Commodities::CDateInterval::CDateInterval (QuantLib::DateInterval& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phDateInterval = NULL;
#endif
	_ppDateInterval = new boost::shared_ptr<QuantLib::DateInterval> (&childNative);
    
    _DateIntervalOwner = owner;
    
}

Cephei::QL::Experimental::Commodities::CDateInterval::CDateInterval (CDateInterval^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phDateInterval = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppDateInterval = new boost::shared_ptr<QuantLib::DateInterval> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Commodities::CDateInterval::CDateInterval (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phDateInterval = NULL;
#endif
	if (!t->IsSubclassOf(CDateInterval::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Commodities::CDateInterval::CDateInterval (QuantLib::Handle<QuantLib::DateInterval>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phDateInterval = &childNative;
	_ppDateInterval = &static_cast<boost::shared_ptr<QuantLib::DateInterval>>(childNative.currentLink());
    
    _DateIntervalOwner = owner;
}
Cephei::QL::Experimental::Commodities::CDateInterval::CDateInterval (QuantLib::Handle<QuantLib::DateInterval> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phDateInterval = &childNative;
	_ppDateInterval = &static_cast<boost::shared_ptr<QuantLib::DateInterval>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Commodities::CDateInterval::CDateInterval (QuantLib::DateInterval childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phDateInterval = NULL;
#endif
	_ppDateInterval = new boost::shared_ptr<QuantLib::DateInterval> (new QuantLib::DateInterval (childNative));
    
}
#endif

Cephei::QL::Experimental::Commodities::CDateInterval::~CDateInterval ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppDateInterval != NULL)
    {
	    delete _ppDateInterval;
        _ppDateInterval = NULL;
    }
}
Cephei::QL::Experimental::Commodities::CDateInterval::!CDateInterval ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppDateInterval != NULL)
    {
	    delete _ppDateInterval;
    }
}
QuantLib::DateInterval& Cephei::QL::Experimental::Commodities::CDateInterval::GetReference ()
{
    if (_ppDateInterval == NULL) throw gcnew NativeNullException ();
	return **_ppDateInterval;
}
boost::shared_ptr<QuantLib::DateInterval>& Cephei::QL::Experimental::Commodities::CDateInterval::GetShared ()
{
    if (_ppDateInterval == NULL) throw gcnew NativeNullException ();
	return *_ppDateInterval;
}
QuantLib::DateInterval* Cephei::QL::Experimental::Commodities::CDateInterval::GetPointer ()
{
    if (_ppDateInterval == NULL) throw gcnew NativeNullException ();
	return &**_ppDateInterval;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::DateInterval>& Cephei::QL::Experimental::Commodities::CDateInterval::GetHandle ()
{
	if (_phDateInterval == NULL)
	{
		_phDateInterval = new Handle<QuantLib::DateInterval> (*_ppDateInterval);
	}
	return *_phDateInterval;
}
#endif
bool Cephei::QL::Experimental::Commodities::CDateInterval::HasNative () 
{
	return (_ppDateInterval != NULL);
}

DateTime Cephei::QL::Experimental::Commodities::CDateInterval::EndDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppDateInterval)->endDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Commodities::IDateInterval^ Cephei::QL::Experimental::Commodities::CDateInterval::Intersection (Cephei::QL::Experimental::Commodities::IDateInterval^ di)
{
    CDateInterval^ _Cdi;
    try
    {
        _Cdi = safe_cast<CDateInterval^> (di);
        _Cdi->Lock();
        QuantLib::DateInterval& _di = static_cast<QuantLib::DateInterval&> (_Cdi->GetReference ()); 
    	QuantLib::DateInterval _rv = (QuantLib::DateInterval)(*_ppDateInterval)->intersection ( _di );   
        Cephei::QL::Experimental::Commodities::CDateInterval^ _nrv = gcnew Cephei::QL::Experimental::Commodities::CDateInterval (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cdi != nullptr) _Cdi->Unlock();
    }
}
Boolean Cephei::QL::Experimental::Commodities::CDateInterval::IsDateBetween (DateTime date, Microsoft::FSharp::Core::FSharpOption<Boolean>^ includeFirst, Microsoft::FSharp::Core::FSharpOption<Boolean>^ includeLast)
{
    try
    {
        QuantLib::Date _date = (QuantLib::Date)ValueHelper::Convert (date);
        bool _includeFirst = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (includeFirst) ? (bool)ValueHelper::Convert (includeFirst->Value) : true); //9a
        bool _includeLast = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (includeLast) ? (bool)ValueHelper::Convert (includeLast->Value) : true); //9a
    	bool _rv = (bool)(*_ppDateInterval)->isDateBetween ( _date,  _includeFirst,  _includeLast );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Experimental::Commodities::CDateInterval::StartDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppDateInterval)->startDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Commodities::IDateInterval^ Cephei::QL::Experimental::Commodities::CDateInterval_Factory::Create (DateTime startDate, DateTime endDate)
{
    return gcnew CDateInterval ( startDate,  endDate);
}
Cephei::QL::Experimental::Commodities::IDateInterval^ Cephei::QL::Experimental::Commodities::CDateInterval_Factory::Create ()
{
    return gcnew CDateInterval ();
}
